#pragma once

#include "Trigger.h"

#include "Event.h"
#include "Port.h"

namespace uml {

Trigger::Trigger(const QString& umlType)
    : NamedElement(umlType) {}

void Trigger::fromJson(const QJsonObject& json, QStringList& errorList) {
    if (json.isEmpty()) {
        return;
    }

    NamedElement::fromJson(json, errorList);
    event_ = JsonSerializable::create<Event>(json.value("event"), errorList);

    JsonSerializable::fromJsonArray(ports_, "ports", json, errorList);
}

void Trigger::toJson(QJsonObject& json) {
    NamedElement::toJson(json);
    if (event_) {
        QJsonObject event_json;
        event_->toJson(event_json);
        json.insert("event", event_json);
    }
    JsonSerializable::toJsonArray(ports_, "ports", json);
}

/// <summary>
/// event getter
/// The Event that detected by the Trigger.
/// </summary>
shared_ptr<Event> Trigger::event() const {
    return event_;
}

/// <summary>
/// event setter
/// The Event that detected by the Trigger.
/// </summary>
void Trigger::setEvent(shared_ptr<Event> value) {
    event_ = value;
}

/// <summary>
/// ports getter
/// A optional Port of through which the given effect is detected.
/// </summary>
vector<shared_ptr<Port>>& Trigger::ports() {
    return ports_;
}

} // namespace uml
